package com.kf.bigbluebook.controller;

import com.kf.bigbluebook.common.BaseResponse;
import com.kf.bigbluebook.common.DeleteRequest;
import com.kf.bigbluebook.common.ErrorCode;
import com.kf.bigbluebook.common.ResultUntils;
import com.kf.bigbluebook.exception.BusinessException;
import com.kf.bigbluebook.model.dto.TeamQuery;
import com.kf.bigbluebook.model.po.Team;
import com.kf.bigbluebook.model.po.User;
import com.kf.bigbluebook.model.po.UserTeam;
import com.kf.bigbluebook.model.request.TeamAddRequest;
import com.kf.bigbluebook.model.request.TeamJoinRequest;
import com.kf.bigbluebook.model.request.TeamQuitRequest;
import com.kf.bigbluebook.model.request.TeamUpdateRequest;
import com.kf.bigbluebook.model.vo.TeamUserVO;
import com.kf.bigbluebook.service.TeamService;
import com.kf.bigbluebook.service.UserService;
import com.kf.bigbluebook.service.UserTeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/team")
@Api(tags = "队伍管理")
@CrossOrigin(origins = "http://localhost:3000", allowCredentials = "true")
public class TeamController {
    @Autowired
    private TeamService teamService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserTeamService userTeamService;

    /***
     * 添加队伍
     * @param originalTeam
     * @param request
     * @return
     */
    @ApiOperation(value = "添加队伍")
    @PostMapping("/add")
    public BaseResponse<Long> teamAdd(@RequestBody TeamAddRequest originalTeam, HttpServletRequest request){
        //判断参数是否为空
        if (originalTeam == null){
            throw new BusinessException(ErrorCode.PARAM_ERROR,"参数为空");
        }
        User loginUser = userService.getLoginUser(request);
        Team team = new Team();
        BeanUtils.copyProperties(originalTeam,team);
        team.setUserId(loginUser.getId());
        Long teamId = teamService.addTeam(team,loginUser);
        return ResultUntils.success(teamId);
    }

    /**
     * 查询队伍信息
     * @param teamQuery
     * @param request
     * @return
     */
    @ApiOperation(value = "查询队伍")
    @GetMapping("/list")
    public BaseResponse<List<TeamUserVO>> listTeams(TeamQuery teamQuery, HttpServletRequest request) {
        //判断参数是否为空
        if (teamQuery == null || request == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }

        List<TeamUserVO> teamList = teamService.listTeams(teamQuery,request);
        return ResultUntils.success(teamList);
    }
    @GetMapping("/get")
    public BaseResponse<Team> getTeamById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        Team team = teamService.getById(id);
        if (team == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        return ResultUntils.success(team);
    }

    /**
     * 更新队伍信息
     * @param teamUpdateRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "更新队伍信息")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeam(@RequestBody TeamUpdateRequest teamUpdateRequest, HttpServletRequest request) {
        if (teamUpdateRequest == null){
            throw new BusinessException(ErrorCode.PARAM_ERROR,"参数为空");
        }
        User loginUser = userService.getLoginUser(request);
        Boolean result = teamService.updateTeam(teamUpdateRequest,loginUser);
        if (result == null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"更新失败");
        }
        return ResultUntils.success(result);
    }

    /**
     * 加入队伍
     * @param teamJoinRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "加入队伍")
    @PostMapping("/join")
    public BaseResponse<Boolean> joinTeam(@RequestBody TeamJoinRequest teamJoinRequest, HttpServletRequest request) {
        if (teamJoinRequest == null){
            throw new BusinessException(ErrorCode.PARAM_ERROR,"参数为空");
        }
        User loginUser = userService.getLoginUser(request);
        Boolean result = teamService.joinTeam(teamJoinRequest,loginUser);
        return ResultUntils.success(result);
    }

    /**
     * 退出队伍
     * @param teamQuitRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "退出队伍")
    @PostMapping("/quit")
    public BaseResponse<Boolean> quitTeam(@RequestBody TeamQuitRequest teamQuitRequest, HttpServletRequest request) {
        if (teamQuitRequest == null){
            throw new BusinessException(ErrorCode.PARAM_ERROR,"参数为空");
        }
        User loginUser = userService.getLoginUser(request);
        Boolean result = teamService.quitTeam(teamQuitRequest,loginUser);
        return ResultUntils.success(result);
    }

    /**
     * 删除队伍
     * @param deleteRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "删除队伍")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeam(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() < 0){
            throw new BusinessException(ErrorCode.PARAM_ERROR,"参数为空");
        }
        User loginUser = userService.getLoginUser(request);
        Boolean result = teamService.deleteTeam(deleteRequest,loginUser);
        return ResultUntils.success(result);
    }

    /***
     * 获取我创建的队伍
     * @param teamQuery
     * @param request
     * @return
     */
    @ApiOperation(value = "获取我创建的队伍")
    @GetMapping("/list/my/create")
    public BaseResponse<List<TeamUserVO>> listMyCreateTeams(TeamQuery teamQuery, HttpServletRequest request) {
        if (teamQuery == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        List<TeamUserVO> teamList = teamService.listTeams(teamQuery, request);
        return ResultUntils.success(teamList);
    }
    @ApiOperation(value = "获取加入的队伍")
    @GetMapping("/list/my/join")
    public BaseResponse<List<TeamUserVO>> listMyJoinTeams(TeamQuery teamQuery, HttpServletRequest request) {
        if (teamQuery == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        List<UserTeam> userTeamList = userTeamService.lambdaQuery().eq(UserTeam::getUserId, loginUser.getId()).list();
        Map<Long, List<UserTeam>> listMap = userTeamList.stream()
                .collect(Collectors.groupingBy(UserTeam::getTeamId));
        List<Long> idList = new ArrayList<>(listMap.keySet());
        teamQuery.setIdList(idList);
        List<TeamUserVO> teamList = teamService.listTeams(teamQuery, request);
        return ResultUntils.success(teamList);
    }
}
